<?php
/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *       -------------------APP 搜索功能-----------------------
 *      $Id: index.php 31307 2015-08-07 1:24 安徽零度网络科技 张园  $
 */


if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
$baseurl=$_SERVER['SERVER_NAME'];
$keyword=urldecode(iconv("UTF-8", "GB2312//IGNORE",$_GET['keyword']));

//帖子json请求
if($_GET['action']=='jsonlist'){ GetPost($keyword);exit;}

//用户json请求
if($_GET['action']=='jsonuser'){ GetUser($keyword,$baseurl);exit; }


//用户列表模板加载
if($_GET['action']=='userlist'){
include template('search/searchlist');
exit;
}



$table=DB::table("common_member");
$userlist=DB::fetch_all("SELECT  {$table}.`uid`,{$table}.`username`,{$table}.`avatarstatus` FROM `{$table}` WHERE `username` LIKE '%{$keyword}%' ORDER BY `{$table}`.`uid` DESC  LIMIT 0 , 3");
if(empty($userlist)){ 
  $nodata='1';
}
include template('search/index');

//获取用户
function GetUser($key,$baseurl){
    $last = $_GET['last']; 
    $amount = $_GET['amount']; 
    $member=DB::table("common_member");
	$group=DB::table("common_usergroup");
    $sql="SELECT {$member}.`uid` , {$member}.`username` , {$member}.`avatarstatus` ,  `{$group}`.`grouptitle` FROM  `{$member}` ,  `{$group}`  WHERE  `{$member}`.`username` LIKE  '%{$key}%' AND  `{$member}`.`groupid` = `{$group}`.`groupid`    ORDER BY `{$member}`.`uid` DESC LIMIT {$last},{$amount}";
	$query=DB::query($sql);
	$i='0';
	while($arr = DB::fetch($query)){
	   if($arr['avatarstatus']=='1'){
	   $photo="<img src=\"http://{$baseurl}/uc_server/data/avatar/000/00/00/0{$arr['uid']}_avatar_middle.jpg\" class=\"userps\" />";
	   }else{
	   $photo="<img src=\"http://{$baseurl}/uc_server/images/noavatar_small.gif\" class=\"userps\" />";
	   }
	  $list["data"]="<div class=\"ulist\" onclick=\"GoUserInfo('{$arr['uid']}')\"><div class=\"user listuser\">{$photo}</div><div class=\"userinfo\"><dt style=\"height:17px;\"></dt> <dt class=\"nick\">{$arr['username']}</dt><dt style=\"height:10px;\"></dt><dt><span class=\"leves\">{$arr['grouptitle']}</span></dt></div>";
	  $data[$i++]=$list;
	   
	}
	echo json_encode(icon_to_utf8($data));
}


//获取帖子
function GetPost($key){
    $last = $_GET['last']; 
    $amount = $_GET['amount']; 
    $post=DB::table("forum_post");
    $forum=DB::table("forum_forum");
    $sql="SELECT {$post}.`dateline`,{$post}.`author` ,{$post}.`tid`,{$post}.`fid`, {$post}.`subject`, {$post}.`message`,{$forum}.`name` FROM {$post},{$forum}  WHERE `subject` LIKE '%{$key}%' and {$post}.`fid`={$forum}.`fid`  order by {$post}.`pid` desc limit {$last},{$amount}";
    $query=DB::query($sql);
    $i='0';
    while($arr = DB::fetch($query)) {
		$str_a=array($key);
		$str_b=array("<font color=red>{$key}</font>");
		$subject=str_replace($str_a,$str_b,$arr['subject']);
		$time=format_date($arr['dateline']);
		$list["data"]="<div class=\"link\" onclick=\"Goview('{$arr['tid']}')\"><dt>{$subject}</dt><dt  class=\"smalltitle\"><span>{$arr['name']}</span><span>{$time}</span><span>{$arr['author']}</span></dt></div>";
        $data[$i++]=$list;
    }
	 if(empty($data)){
		 $data['data']="<div class=\"nodata\">暂无数据</div>";
		 
	  }
	      echo json_encode(icon_to_utf8($data));  
	
}

//解决GBK编码json无法解析
function ct2($s){
    if(is_numeric($s)) {
        return intval($s);
    } else {
        return iconv("GBK","UTF-8",$s);
    }
}
function icon_to_utf8($s) {
  if(is_array($s)) {
    foreach($s as $key => $val) {
      $s[$key] = icon_to_utf8($val);
    }
  } else {
      $s = ct2($s);
  }
  return $s;
}

//发帖时间计算
function format_date($time){
    $t=time()-$time;
    $f=array(
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.'前';
        }
    }
}
?>
